using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Geekeradmin.Entitys;

namespace Geekeradmin.Filters
{
    /// <summary>
    /// 结果过滤器
    /// </summary>
    public class CustomResultAsync : Attribute, IAsyncResultFilter
    {
        private readonly ILogger<CustomResultAsync> logger;
        public CustomResultAsync(ILogger<CustomResultAsync> logger)
        {
            this.logger = logger;
        }

        public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
        {
            if (context.Result is JsonResult)
            {
                JsonResult result = (JsonResult)context.Result;
                context.Result = new JsonResult(new AjaxResult()
                {
                    Success = true,
                    Message = "OK",
                    Data = result.Value,
                    dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                });
            }
            await next.Invoke();
        }
    }
}
